package com.lfg.leetcode.leetcode0;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class No12IntToRoman {


    private static int[] intArray = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    private static String[] charArray = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};


    public static String intToRoman(int num) {
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < intArray.length; i++) {
            while (num >= intArray[i]) {
                ret.append(charArray[i]);
                num = num - intArray[i];
            }
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        System.out.println(intToRoman(1666));
        System.out.println(intToRoman(9));
    }

    public static String intToRoman2(int num) {
        int NUM1[] = {1000, 100, 10, 1};
        int NUM[] = {1000, 500, 100, 50, 10, 5, 1};
        Map<Integer, String> DATA = new HashMap<Integer, String>();
        DATA.put(1000, "M");
        DATA.put(500, "D");
        DATA.put(100, "C");
        DATA.put(50, "L");
        DATA.put(10, "X");
        DATA.put(5, "V");
        DATA.put(1, "I");

        int j = 6;
        StringBuilder ret = new StringBuilder();
        List<Integer> list = new ArrayList<Integer>(10);
        for (int i = 0; i < NUM1.length; i++) {
            int length = num / NUM1[i];
            num = num % NUM1[i];
            list.add(length);
        }
        for (int i = list.size() - 1; i >= 0; i--) {
            int length = list.get(i);
            if (length == 4) {
                ret.insert(0, DATA.get(NUM[j - 1]));
                ret.insert(0, DATA.get(NUM[j]));
            } else if (length == 9) {
                ret.insert(0, DATA.get(NUM[j - 2]));
                ret.insert(0, DATA.get(NUM[j]));
            } else if (length >= 5) {
                ret.insert(0, DATA.get(NUM[j - 1]));
                while (length - 5 > 0) {
                    ret.insert(1, DATA.get(NUM[j]));
                    length--;
                }
            } else {
                while (length > 0) {
                    ret.insert(0, DATA.get(NUM[j]));
                    length--;
                }
            }
            j -= 2;
        }
        return ret.toString();
    }
}
